package cn.tedu.boot.ers.service.impl;

import cn.tedu.boot.ers.entity.Admin;
import cn.tedu.boot.ers.entity.User;
import cn.tedu.boot.ers.exception.*;
import cn.tedu.boot.ers.mapper.AdminMapper;
import cn.tedu.boot.ers.pojo.dto.AdminAddDTO;
import cn.tedu.boot.ers.pojo.dto.AdminLoginDTO;
import cn.tedu.boot.ers.pojo.dto.AdminUpdateNameDTO;
import cn.tedu.boot.ers.pojo.dto.AdminUpdatePasswordDTO;
import cn.tedu.boot.ers.pojo.vo.AdminSimpleVO;
import cn.tedu.boot.ers.service.IAdminService;
import cn.tedu.boot.ers.util.PasswordUtils;
import cn.tedu.boot.ers.web.State;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import javax.xml.bind.annotation.XmlAnyAttribute;

@Service
@Slf4j
public class AdminServiceImpl implements IAdminService {
    @Autowired
    AdminMapper mapper;

    // 根据name查询表格里面有无当前插入管理员的信息
    // 根据tel查询表格里面有无当前插入管理员的信息
    @Override
    public void addNew(AdminAddDTO adminAddDTO) {
        log.info(adminAddDTO.toString());
        // 自动装配mapper
        // 根据name查询表格里面有无当前插入管理员的信息
        // 根据tel查询表格里面有无当前插入管理员的信息
        String name = adminAddDTO.getName();
        String tel = adminAddDTO.getTel();
        if (mapper.getByName(name) != null || mapper.getByTel(tel) != null) {
            log.error("插入失败用户({})已存在", name);
            throw new ServiceException(State.ERR, "用户已存在,名字或电话号码重复!");
        } else {
            // 创建Admin对象
            Admin admin = new Admin();
            // 给Admin对象赋值
            String password = adminAddDTO.getPassword();
            password = PasswordUtils.encodePassword(password);
            adminAddDTO.setPassword(password);
            BeanUtils.copyProperties(adminAddDTO, admin);
            // 补全Admin属性
            // 根据AdminMapper mapper.insert(Admin),添加管理员
            int rows = mapper.insert(admin);
            log.info("插入行数:" + rows);
            if (rows != 1) {
                log.error("插入失败,服务器忙");
                throw new ServiceException(State.ERR, "添加失败,服务器忙,请稍后再试");
            }
        }
    }

    @Override
    public void updateNameById(AdminUpdateNameDTO adminUpdateNameDTO) {
        // 调用mapper.updateNameById(adminUpdateNameDTO)
        String name = adminUpdateNameDTO.getName();
        AdminSimpleVO adminSimpleVO = mapper.getByName(name);
        if (adminSimpleVO != null) {
            throw new ServiceException(State.ERR, "名字已存在,请更改其他名字");
        }
        int rows = mapper.updateNameById(adminUpdateNameDTO);
        if (rows != 1) {
            throw new ServiceException(State.ERR, "服务器忙,请稍后再试");
        }

    }

    @Override
    public void updatePassword(AdminUpdatePasswordDTO adminUpdatePasswordDTO) {
        Long id = adminUpdatePasswordDTO.getId();
        AdminSimpleVO adminSimpleVO = mapper.getById(id);
        // 得到输入原始密码
        String rawPassword = adminUpdatePasswordDTO.getRawPassword();
        // 将输入的原始密码与数据库里面存的密码作比较
        boolean matches = PasswordUtils.matches(rawPassword, adminSimpleVO.getPassword());
        if (!matches) {
            throw new ServiceException(State.ERR, "原密码错误,请重新输入");
        }
        String password = adminUpdatePasswordDTO.getPassword();
        String passwordEncoded = PasswordUtils.encodePassword(password);
        adminUpdatePasswordDTO.setPassword(passwordEncoded);
        int rows = mapper.updatePasswordById(adminUpdatePasswordDTO);
        if (rows != 1) {
            throw new ServiceException(State.ERR, "服务器忙,密码修改失败");
        }
    }

    @Override
    public void login(AdminLoginDTO adminLoginDTO, HttpSession session) {
        log.info(adminLoginDTO.toString());
        // 获取登录的管理员名字name或tel
        String name = adminLoginDTO.getName();
        String tel = adminLoginDTO.getTel();
        String password = adminLoginDTO.getPassword();
        // 通过name或者tel到数据库去查找相关信息
        AdminSimpleVO admin0 = mapper.getByTel(tel);
        AdminSimpleVO admin1 = mapper.getByName(name);
        if (admin0 == null && admin1 == null) {
            log.error("用户不存在");
            // 如果为null则抛出异常
            throw new ServiceException(State.ERR, "用户名不存在");
        } else {
            admin0 = ((admin0 == null) ? admin1 : admin0);
            log.info(admin0.toString());
            // 判断当前用户的isEnable,如果为1,则可以继续登录操作;如国不为1,则不可以登录
            if (admin0.getIsEnable().equals("1")) {
                // 如果密码匹配,则登录成功
                boolean matches = PasswordUtils.matches(password, admin0.getPassword());
                if (matches) {
                    admin0.setPassword("");
                    admin0.setTel("");
                    // 目的,管理员登录后,必要的信息通过HttpSession保存在会话中
                    session.setAttribute("admin", admin0);
                    return;
                }
                throw new ServiceException(State.ERR, "密码错误");
            }
            throw new ServiceException(State.ERR, "管理员状态异常,请联系老板");
        }
    }

    @Override
    public boolean checkAdminRole(Admin admin) {
        //0是普通用户，1是管理员
        return admin.getLevel().equals(1);
    }

}
